package com.yangzb.java.sample.thread;

import cn.hutool.core.util.RandomUtil;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author yzb
 */
public class QueueTest {
    public static void main(String[] args) throws InterruptedException {
        LinkedBlockingQueue queue=new LinkedBlockingQueue<Runnable>();
        ThreadPoolExecutor cachedThreadPool = new ThreadPoolExecutor(20, 20,
                0L, TimeUnit.MILLISECONDS,
                queue);

        while (true){
//            System.out.println(queue.size());
            cachedThreadPool.submit(() -> {
                try {
                    Thread.sleep(RandomUtil.randomInt(1,1000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            if(queue.size()>20){
                Thread.sleep(100);
                System.out.println("sleep");
                System.out.println(queue.size());
            }
        }

    }
}
